package com.wfh.activity;

import java.util.ArrayList;
import java.util.zip.Inflater;

import com.wfh.R;
import com.wfh.R.layout;
import com.wfh.R.menu;
import com.wfh.adapter.MyTicketsAdapter;
import com.wfh.adapter.TicketsAdapter;
import com.wfh.bean.TicketsBean;
import com.wfh.util.MyDialog;
import com.wfh.util.Util;

import android.os.Bundle;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MyTicketsActivity extends CommonActivity {
	private MyDialog dialog, alert;
	private ArrayList<TicketsBean> list;
	private MyTicketsAdapter adapter;
	private ListView listview;
	private boolean mbundle = true;
	private BroadcastReceiver datareceiver;
	private Button back, tickets, weishiyong, yishiyong, yiguoqi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, R.layout.activity_mytickets);

		listview = (ListView) findViewById(R.id.listView1);
		dialog = MyDialog.create(this);
		back = (Button) findViewById(R.id.back);
		tickets = (Button) findViewById(R.id.tickets);
		weishiyong = (Button) findViewById(R.id.weishiyong);
		yishiyong = (Button) findViewById(R.id.yishiyong);
		yiguoqi = (Button) findViewById(R.id.yiguoqi);
		
		weishiyong.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
				Intent intent=new Intent();
				intent.putExtra("status", "0");
				HomeActivity.service.getMyLocalTickets(MyTicketsActivity.this, intent);
				intent=null;
			}	
		});
		
		yishiyong.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
				Intent intent=new Intent();
				intent.putExtra("status", "1");
				HomeActivity.service.getMyLocalTickets(MyTicketsActivity.this, intent);
				intent=null;
			}	
		});
		
		yiguoqi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.show();
				Intent intent=new Intent();
				intent.putExtra("status", "2");
				HomeActivity.service.getMyLocalTickets(MyTicketsActivity.this, intent);
				intent=null;
			}	
		});
		
		
		
		// 返回
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MyTicketsActivity.this.finish();
			}
		});

		// 返回列表
		tickets.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Util.startactivity(MyTicketsActivity.this,
						TicketsActivity.class);
				MyTicketsActivity.this.finish();
			}
		});

		// 注册recevier广播接收器
		IntentFilter filter = new IntentFilter();
		filter.addAction(Util.D_MYTICKETS);
		datareceiver = new DataReceiver();
		registerReceiver(datareceiver, filter);

		dialog.show();
		HomeActivity.service.getMyLocalTickets(this, null);

	}

	// 接收器
	private class DataReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bd = intent.getExtras();
			if (bd.getBoolean("isnull")) {
				dialog.hide();
				View view;
				LayoutInflater minflater = LayoutInflater
						.from(MyTicketsActivity.this);
				view = minflater.inflate(R.layout.tickets_dialog, null);
				Button btn = (Button) view.findViewById(R.id.button1);
				// 注册事件
				btn.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Util.startactivity(MyTicketsActivity.this,
								TicketsActivity.class);
						MyTicketsActivity.this.finish();
					}
				});
				alert = MyDialog.create(MyTicketsActivity.this, view);
				alert.show();
				return;
			}
			list = bd.getParcelableArrayList("mytickets");
			if (list == null || list.size() == 0){
				list=new ArrayList<TicketsBean>();
				list.clear();
				adapter.list=list;
				adapter.notifyDataSetChanged();
				dialog.hide();
				return;
			}
			if (adapter != null) {
				adapter.list = list;
				adapter.notifyDataSetChanged();
				if (alert != null)
					alert.hide();
				dialog.hide();
				mbundle = true;
				return;
			}
			adapter = new MyTicketsAdapter(MyTicketsActivity.this, list);
			listview.setAdapter(adapter);
//			Util.setListViewHeightBasedOnChildren(listview);
			dialog.hide();
			if (alert != null)
				alert.hide();
			mbundle = true;
		}
	}

	@Override
	protected void onDestroy() {
		if (dialog != null)
			dialog.cancel();
		if (alert != null)
			alert.cancel();
		unregisterReceiver(datareceiver);
		super.onDestroy();
	}

}
